package com.xuecheng.content.controller;

import com.xuecheng.api.content.model.FileManagerApi;
import com.xuecheng.api.content.model.file.UploadTokenResult;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.RandomStringUtil;
import com.xuecheng.content.common.constant.ContentErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
public class FileManagerController implements FileManagerApi {

    @Autowired
    private RestTemplate restTemplate;

    //nacos配置
//    #对文件系统微服务接口所需参数的配置：
//            #文件系统微服的请求地址
//    file.service.url= http://127.0.0.1:56082/farming/generatetoken?origin=qiniu
//            #文件存储空间名称
//    file.service.bucket= xc-136-static-images
//
//#对前端所需参数的配置
//#文件存储区域的地址
//    file.service.upload.region = http://upload.qiniu.com
//            #文件访问的cdn加速域名
//    cdn.domain = r402e6j10.hd-bkt.clouddn.com
//
//    file.token.type = 1
//    file.token.deadline = 3600

    @Value("${file.service.url}")
    private String url;
    @Value("${file.service.bucket}")
    private String bucket;
    @Value("${file.service.upload.region}")
    private String region;
    @Value("${cdn.domain}")
    private String domain;
    @Value("${file.token.type}")
    private String type;
    @Value("${file.token.deadline}")
    private int deadline;

    @GetMapping("common/qnUploadToken")
    public UploadTokenResult qiniuUploadToken() {
        //1.发送请求获得文件上传的凭证
        HashMap<String, Object> requestBody = new HashMap<>();
        requestBody.put("tokenType", type);
        requestBody.put("scope", bucket);
        requestBody.put("deadline", deadline);

        //上传文件的key（唯一）
        String fileKey = UUID.randomUUID().toString() + RandomStringUtils.randomAlphabetic(16);


        requestBody.put("key", fileKey);

        ResponseEntity<Map> responseEntity = restTemplate.postForEntity(url, requestBody, Map.class);

        int statusCodeValue = responseEntity.getStatusCodeValue();

        if (!(HttpStatus.OK.value()==statusCodeValue)) {
            ExceptionCast.cast(ContentErrorCode.E_120022);
        }

        Map body = responseEntity.getBody();

        //2.将结果封装成前端所需要的内容并返回

        UploadTokenResult tokenResult = new UploadTokenResult();

        //xc-api下UploadTokenResult类中数据
        tokenResult.setTokenType(type);
        tokenResult.setScope(bucket);
        tokenResult.setKey(fileKey);
        tokenResult.setQnToken(body.get("result").toString());
        tokenResult.setUp_region(region);
        tokenResult.setDomain(domain);
        tokenResult.setDeadline(deadline);

        return tokenResult;
    }
}
